/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.azon.left.hand;

/**
 *
 * @author Mahdan Ahmad F A
 */
public class NumberGuesser extends Guesser{

    @Override
    public String doGueess(String chain_code) {
        String number = "";
        
        String  code        = getChainCode(chain_code);
        int[]   frequency   = getFrequency(chain_code);
        String  sequence    = getSequence(chain_code);
        
        int kurvaCembungKanan   = cekKurvaCembungKanan(sequence);
        int kurvaCembungKiri    = cekKurvaCembungKiri(sequence);
        
        if ((kurvaCembungKanan == 2) && kurvaCembungKiri == 2) {
            // 8
            number = "Angka Delapan";
        } else if ((kurvaCembungKanan == 2) && kurvaCembungKiri == 0) {
            // 3
            number = "Angka Tiga";
        } else if ((kurvaCembungKanan == 1) && kurvaCembungKiri == 1) {
            //0, 6, 9
            if (maybeNine(code, sequence, frequency)) {
                number = "Angka Sembilan";
            } else if(maybeSix(code, sequence, frequency)) {
                number = "Angka Enam";
            } else if (maybeZero(code, sequence, frequency)) {
                number = "Angka Nol";
            }
        } else if ((kurvaCembungKanan == 1) && kurvaCembungKiri == 0) {
            // 5
            number = "Angka Lima";
        } else if (((kurvaCembungKanan == 0) && kurvaCembungKiri == 0)) {
            //1, 2, 4, 7
            int garisLurus  = cekGarisLurus(code);
            int garisMiring = cekGarisMiring(sequence);
            
            if ((garisLurus == 2) && (garisMiring == 2)) {
                number = "Angka Tujuh";
            } else if (((garisLurus == 3) || (garisLurus == 4)) && (garisMiring == 1)) {
                number = "Angka Empat";
            } else if (((garisLurus == 4) && (garisMiring == 2)) || (garisLurus == 2)) {
                number = "Angka Satu";
            } else {
                number = "Angka Dua";
            }
            
        }
        
        return number;
    }
    
    private boolean maybeZero(String code, String sequence, int[] frequence) {
        String anotherSequence = "";
        
        for(int i = 0; i < sequence.length(); i++) {
            if (!anotherSequence.contains("" + sequence.charAt(i))) {
                anotherSequence += sequence.charAt(i);
            } else {
                
            }
        }
        
        return anotherSequence.equals("69874123") || anotherSequence.equals("98741236");
    }
    
    private boolean maybeOne(String code, String sequence, int[] frequence) {
        
        return false;
    }
    
    private boolean maybeTwo(String code, String sequence, int[] frequence) {
        
        return false;
    }
    
    private boolean maybeThree(String code, String sequence, int[] frequence) {
        
        return false;
    }
    
    private boolean maybeFour(String code, String sequence, int[] frequence) {
        
        return false;
    }
    
    private boolean maybeFive(String code, String sequence, int[] frequence) {
        
        return false;
    }
    
    private boolean maybeSix(String code, String sequence, int[] frequence) {
        
        String another = sequence;
        
        while(another.indexOf("414141") != -1) {
            another = another.substring(another.indexOf("41"));
        
            while(another.startsWith("41")) {
                another = another.substring(2);
            }

            if (another.startsWith("474747")) {
                while(another.startsWith("47")) {
                    another = another.substring(2);
                }

                if (another.startsWith("878787")) {
                    while(another.startsWith("87")) {
                        another = another.substring(2);
                    }

                    return true;
                }
            }
        }
        
        return false;
    }
    
    private boolean maybeSeven(String code, String sequence, int[] frequence) {
        
        return false;
    }
    
    private boolean maybeEight(String code, String sequence, int[] frequence) {
        
        return false;
    }

    private boolean maybeNine(String code, String sequence, int[] frequence) {
        
        String another = sequence;
        
        while(another.indexOf("696969") != -1) {
            another = another.substring(another.indexOf("69"));
        
            while(another.startsWith("69")) {
                another = another.substring(2);
            }

            if (another.startsWith("636363")) {
                while(another.startsWith("63")) {
                    another = another.substring(2);
                }

                if (another.startsWith("232323")) {
                    while(another.startsWith("23")) {
                        another = another.substring(2);
                    }

                    return true;
                }
            }
        }
        
        return false;
    }
    
}
